package aptech.lab.servlet.context;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Application Lifecycle Listener implementation class AppListener
 *
 */
@WebListener
public class AppListener implements ServletContextListener {
	private ServletContext context = null;
    /**
     * Default constructor. 
     */
    public AppListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent event) {
    	System.out.println("AppListener contextInitialized");
        context = event.getServletContext();
        context.setAttribute("Counter", new Integer(0)); 
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent event) {
    	System.out.println("AppListener contextDestroyed");
    	context = event.getServletContext();
        context.log ((String)context.getAttribute("Counter"));
        context.removeAttribute("Counter"); 
    }
}
